package Util::Store::StoreUtil;
use Moose;
use namespace::autoclean;
use JSON;
use Time::Piece;
use Time::Seconds;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub calculate_yuji_tihuo_time {
	my ($self, $on_going) = @_;
	
	my %tuanTimes_hash;
        foreach (@$on_going) {
                my $yuji_tihuo_time = $_->yuji_tihuo_time;
                if($yuji_tihuo_time and $yuji_tihuo_time =~ /\|/) {
                my @dingqi_tihuo_times =split(/|/,$yuji_tihuo_time);

                my $t = localtime;
                my $current_day_of_week = $t->day_of_week;
                my $next_weekday;
                my $current_next_weekday = 8;
                my $min_weekday = 7;
                my $is_next_week = 0;
                my $day_count = 0;
                foreach(@dingqi_tihuo_times) {
                        if($day_count % 2 == 1) {
                                if($_ > $current_day_of_week) {
                                        $next_weekday = $_;
                                        if($next_weekday < $current_next_weekday) {
                                                $current_next_weekday = $next_weekday;
                                        }
                                }
                                if($_ < $min_weekday) {
                                        $min_weekday = $_;
                                }
                        }
                        $day_count = $day_count + 1;
                 }
                my $time_diff;
                if($current_next_weekday <= 7) {
                        $time_diff = $current_next_weekday - $current_day_of_week;
                }
                else {
                        $time_diff = $min_weekday + 7 - $current_day_of_week;
                }
                my $yuji_time = localtime;

                while($time_diff > 0) {
                        $yuji_time += ONE_DAY;
                        $time_diff = $time_diff - 1;
                }
                $yuji_tihuo_time = $yuji_time->ymd;
                }
                $tuanTimes_hash{$_->id} = $yuji_tihuo_time;
        }

	return \%tuanTimes_hash;
}

__PACKAGE__->meta->make_immutable;

1;
